package LEETCODE2023;

import java.util.Random;

public class Solution_984 {
    public static void main(String[] args) {
        Solution_984 solution984 = new Solution_984();
        System.out.println(solution984.strWithout3a3b(2, 3));
        System.out.println(new Random().nextInt(100));
    }


    /**
     * 非常聪明的写法
     */
    public String strWithout3a3b1(int a, int b) {
        StringBuilder sb = new StringBuilder();

        while (a > b && b > 0) { sb.append("aab"); --a; --a; --b; }
        while (b > a && a > 0) { sb.append("bba"); --b; --b; --a; }
        while (a > 0 && b > 0) { sb.append("ab"); --a; --b; }
        while (a > 0) { sb.append('a'); --a; }
        while (b > 0) { sb.append('b'); --b; }
        return sb.toString();
    } // strWithout3a3b(int, int)


    /**
     * 超级慢的nt写法
     */
    public String strWithout3a3b(int a, int b) {
        char[] lib = new char[]{'a', 'b'};
        int tag = a > b ? 0 : 1;
        StringBuilder ans = new StringBuilder();
        if (check(a, b)) {
            int len = (int) Math.ceil(Math.max(a, b) * 1.0 / 2);
            int left = Math.min(a, b) - len;
            boolean notFull = Math.max(a, b) % 2 == 1;
            while (len > 0) {
                ans.append(lib[tag]);
                ans.append(lib[tag]);
                if (len == 1) {
                    if (notFull) {
                        ans.deleteCharAt(ans.length() - 1);
                    }
                    if (left < 0) {
                        break;
                    } else {
                        ans.append(lib[(tag + 1) % 2]);
                        break;
                    }
                }
                ans.append(lib[(tag + 1) % 2]);
                len--;
            }
            int index = 0;
            while (left > 0) {
                ans.insert(index, lib[(tag + 1) % 2]);
                left--;
                index = index + 4;
            }
            ;
            return ans.toString();
        } else {
            return null;
        }
    }

    public boolean check(int a, int b) {
        return Math.max(a, b) / (Math.min(a, b) + 1) <= 2;
    }
}
